import 'package:f200/common/index.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class ThemeController extends GetxController {
  static ThemeController get to => Get.find();
  final _box = StorageService.to;
  final _key = 'isDarkMode';

  var isDarkMode = false.obs;

  bool get isDark => theme == ThemeMode.dark;

  ThemeMode get theme => _loadTheme() ? ThemeMode.dark : ThemeMode.light;

  bool _loadTheme() => _box.getBool(_key);

  void saveTheme(bool isDarkMode) => _box.setBool(_key, isDarkMode);

  void changeTheme(ThemeData theme) {
    isDarkMode.value = this.theme == ThemeMode.dark ? false : true;
    Get.changeTheme(theme);
    saveTheme(isDarkMode.value);
    update();
  }

  void changeThemeMode(ThemeMode themeMode) {
    Get.changeThemeMode(themeMode);
    isDarkMode.value = themeMode == ThemeMode.dark;
    if (isDarkMode.value == true) {
      Get.changeTheme(darkTheme());
    } else {
      Get.changeTheme(lightTheme());
    }
    saveTheme(isDarkMode.value);
    update();
  }
}
